package cn.firecode.data_collect.controller;
import	java.awt.Desktop.Action;


import cn.firecode.data_collect.domain.entity.CollectEnvironmentT;
import cn.firecode.data_collect.domain.entity.EnvironmentVi;
import cn.firecode.data_collect.service.CollectEnvironmentTService;
import cn.firecode.data_collect.service.EnvironmentViService;
import cn.firecode.data_collect.utils.DataResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangfan
 * @since 2019-12-10
 */
@RestController
@RequestMapping("/environment")
@Slf4j
public class CollectEnvironmentTController {

    @Autowired
    private CollectEnvironmentTService environmentTService;

    @Autowired
    private EnvironmentViService environmentViService;


    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public DataResponse getList(){
        List<EnvironmentVi> objectList = environmentViService.list();
        return DataResponse.succeed(String.valueOf(objectList.size()), objectList);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public DataResponse add(Integer robotId, Integer deviceId, Integer drillId, Integer drivepipeId, String describe){
        log.info("新增采集环境");
        CollectEnvironmentT environmentT = new CollectEnvironmentT();
        environmentT.setCreateTime(LocalDate.now());
        environmentT.setDescribeInfo(describe);
        environmentT.setStatus(0);
        environmentT.setRobotId(robotId);
        environmentT.setDrivepipeId(drivepipeId);
        environmentT.setDrillId(drillId);
        environmentT.setDeviceId(deviceId);
        boolean save = environmentTService.save(environmentT);
        if(save){
            return DataResponse.succeed("新增成功");
        }else{
            return DataResponse.failed(1, "新增失败");
        }
    }

    @RequestMapping(value = "/delete/{environmentId}" , method = RequestMethod.GET)
    public DataResponse delete(@PathVariable("environmentId") Integer environmentId){
        CollectEnvironmentT environmentT = environmentTService.getById(environmentId);
        if(environmentT == null){
            return DataResponse.failed(1,"删除错误,无有效记录");
        }
        environmentT.setStatus(1);
        boolean result = environmentTService.saveOrUpdate(environmentT);
        if(result){
            return DataResponse.succeed("删除成功");
        }else{
            return DataResponse.failed(1,"删除失败");
        }

    }
}
